<?php
/**
 * tpshop
 * ============================================================================
 * 版权所有 2015-2027 深圳搜豹网络科技有限公司，并保留所有权利。
 * 网站地址: http://www.tp-shop.cn
 * ----------------------------------------------------------------------------
 * Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
 * ============================================================================
 * Author: 当燃      
 * 专题管理
 * Date: 2016-03-09
 */

namespace Admin\Controller;
use Think\AjaxPage;
use Admin\Logic\GoodsLogic;

class ActivityController extends BaseController {

    public function index(){
        $this->display();
    }
  
    //活动页
    public function activity_list(){	
	
		$count = M('activity')->count();
		$Page  = new \Think\Page($count,20);    	 
		$show = $Page->show();               
		$res = M('activity')->order('id desc')->limit($Page->firstRow.','.$Page->listRows)->select();
		if($res){
			foreach ($res as $val){
				if(!empty($val['group']) && !empty($lv)){
					$val['group'] = explode(',', $val['group']);
					foreach ($val['group'] as $v){
						$val['group_name'] .= $lv[$v].',';
					}
				}
				$activity_list[] = $val;
			}
		}
                $this->assign('page',$show);// 赋值分页输出          
		
		$this->assign('activity_list',$activity_list);
		$this->display();
    }
	
	//活动编加页
	
	public function activity_info()
	{
		$activity_id = I('id');
		$info['start_time'] = date('Y-m-d');
		$info['end_time'] = date('Y-m-d',time()+3600*60*24);
		if($activity_id>0){
			$info = M('activity')->where("id=$activity_id")->find();
			$info['start_time'] = date('Y-m-d',$info['start_time']);
			$info['end_time'] = date('Y-m-d',$info['end_time']);
			/*获取当前的产品*/
			//$prom_goods = M('goods')->where("prom_id=$prom_id and prom_type=3")->select();
			//$this->assign('prom_goods',$prom_goods);
			/*获取当前的产品*/
		}
		$this->assign('info',$info);
		$this->assign('min_date',date('Y-m-d'));
		//var_dump($activity_id);exit();
		if($activity_id>0){
			if($info['goods_id'])
			{
				$where['goods_id'] = array('in',$info['goods_id']);
				$prom_goods = M('goods')->where($where)->select();
				$this->assign('prom_goods',$prom_goods);
			}
			
			
			if($info['coupon_id'])
			{
				$where['id'] = array('in',$info['coupon_id']);
				$activity_coupon_list = M('coupon')->where($where)->select();
				$this->assign('activity_coupon_list',$activity_coupon_list);
			}
			
			
			if($info['banner_id'])
			{
				$bannerlist = explode("|",$info['banner_id']);
				$this->assign('bannerlist',$bannerlist);
			}
			
			
			if($info['m_banner_id'])
			{
				$mbannerlist = explode("|",$info['m_banner_id']);
				$this->assign('mbannerlist',$mbannerlist);
			}
		}
		
		$this->initEditor(); // 编辑器
		$this->display();
	}
	
	
	//保存活动
	public function activity_save()
	{
		$activity_id = I('id');
		$data = I('post.');
		$data['start_time'] = strtotime($data['start_time']);
		$data['end_time'] = strtotime($data['end_time']);
		
		if($activity_id){
			//更新
			M('activity')->where("id=$activity_id")->save($data);
			$last_id = $activity_id;
			adminLog("管理员修改了活动管理 ".I('name'));
		}else{
			//插入
			$last_id = M('activity')->add($data);
			adminLog("管理员添加了活动管理 ".I('name'));
		}
		
		//插入产品
		if(is_array($data['goods_id'])){
				$goods_id = implode(',', $data['goods_id']);
				
				if($activity_id>0){
					M("activity")->where("id=$last_id")->save(array('goods_id'=>$goods_id));
				}
				M("activity")->where("id=$last_id")->save(array('goods_id'=>$goods_id));
			}
		//插入优惠券
		
		if(is_array($data['coupon_id'])){
				$coupon_id = implode(',', $data['coupon_id']);
				
				if($activity_id>0){
					M("activity")->where("id=$last_id")->save(array('coupon_id'=>$coupon_id));
				}
				M("activity")->where("id=$last_id")->save(array('coupon_id'=>$coupon_id));
			}
		
		//插入Banner
		if(is_array($data['goods_images'])){
				$goods_images = substr(implode('|', $data['goods_images']),0,-1);
				
				if($activity_id>0){
					M("activity")->where("id=$last_id")->save(array('banner_id'=>$goods_images));
				}
				M("activity")->where("id=$last_id")->save(array('banner_id'=>$goods_images));
			}
			
		if(is_array($data['goods_images_m'])){
				
				$goods_images_m = substr(implode('|', $data['goods_images_m']),0,-1);
				
				if($activity_id>0){
					M("activity")->where("id=$last_id")->save(array('m_banner_id'=>$goods_images_m));
				}
				M("activity")->where("id=$last_id")->save(array('m_banner_id'=>$goods_images_m));
			}
		
		
		$this->success('编辑活动管理成功',U('Activity/activity_list'));
	}


	//选择商品
	public function search_activity_goods(){
    	$GoodsLogic = new \Admin\Logic\GoodsLogic;
    	$brandList = $GoodsLogic->getSortBrands();
    	$this->assign('brandList',$brandList);
    	$categoryList = $GoodsLogic->getSortCategory();
    	$this->assign('categoryList',$categoryList);
    	
    	$goods_id = I('goods_id');
    	$where = ' is_on_sale = 1 ';//选择商品的条件条件
    	if(!empty($goods_id)){
    		$where .= " and goods_id not in ($goods_id) ";
    	}
    	I('intro')  && $where = "$where and ".I('intro')." = 1";
    	if(I('cat_id')){
    		$this->assign('cat_id',I('cat_id'));
    		$grandson_ids = getCatGrandson(I('cat_id'));
    		$where = " $where  and cat_id in(".  implode(',', $grandson_ids).") "; // 初始化搜索条件
    	}
    	if(I('brand_id')){
    		$this->assign('brand_id',I('brand_id'));
    		$where = "$where and brand_id = ".I('brand_id');
    	}
    	if(!empty($_REQUEST['keywords']))
    	{
    		$this->assign('keywords',I('keywords'));
    		$where = "$where and (goods_name like '%".I('keywords')."%' or keywords like '%".I('keywords')."%')" ;
    	}
    	$count = M('goods')->where($where)->count();
    	$Page  = new \Think\Page($count,10000);
    	$goodsList = M('goods')->where($where)->order('goods_id DESC')->limit($Page->firstRow.','.$Page->listRows)->select();
    	$show = $Page->show();//分页显示输出
    	$this->assign('page',$show);//赋值分页输出
    	$this->assign('goodsList',$goodsList);
    	$tpl = I('get.tpl','search_activity_goods');
    	$this->display($tpl);
    }
	
	//选择优惠券
	public function search_activity_coupon(){
    	
    	$coupon_id = I('coupon_id');
		$now = time();
        $where = " send_start_time<$now and send_end_time>$now ";

		
    	if(!empty($coupon_id)){
    		$where .= " and id not in ($coupon_id) ";
    	}
		
    	$count = M('coupon')->where($where)->count();
    	$Page  = new \Think\Page($count,10000);
    	$couponList = M('coupon')->where($where)->order('id DESC')->limit($Page->firstRow.','.$Page->listRows)->select();
    	$show = $Page->show();//分页显示输出
    	$this->assign('page',$show);//赋值分页输出
    	$this->assign('conponList',$couponList);
    	$tpl = I('get.tpl','search_activity_coupon');
    	$this->display($tpl);
    }
    
   
   
	public function activity_del()
	{
		$activity_id = $_GET["id"];
		M('activity')->where(array('id'=>$activity_id))->delete();
        $this->success("操作成功!!!",U('Admin/Activity/activity_list'));
		//$this->success('删除活动成功',U('Activity/activity_list'));
	}
   
    
    private function initEditor()
    {
    	$this->assign("URL_upload", U('Admin/Ueditor/imageUp',array('savepath'=>'promotion')));
    	$this->assign("URL_fileUp", U('Admin/Ueditor/fileUp',array('savepath'=>'promotion')));
    	$this->assign("URL_scrawlUp", U('Admin/Ueditor/scrawlUp',array('savepath'=>'promotion')));
    	$this->assign("URL_getRemoteImage", U('Admin/Ueditor/getRemoteImage',array('savepath'=>'promotion')));
    	$this->assign("URL_imageManager", U('Admin/Ueditor/imageManager',array('savepath'=>'promotion')));
    	$this->assign("URL_imageUp", U('Admin/Ueditor/imageUp',array('savepath'=>'promotion')));
    	$this->assign("URL_getMovie", U('Admin/Ueditor/getMovie',array('savepath'=>'promotion')));
    	$this->assign("URL_Home", "");
    }
    
}